package cn.acmoj.oj.service;

import cn.acmoj.oj.bean.User;
import cn.acmoj.oj.dao.IPrivilegeDao;
import cn.acmoj.oj.dao.IUserDao;
import cn.acmoj.oj.util.SHA;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * cn.acmoj.oj.service
 * Springer
 * 2017-11-09
 */
@Service
public class UserService {

    @Resource
    private IUserDao iUserDao;
    @Resource
    private IPrivilegeDao iPrivilegeDao;

    public User login(String id, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("user_id", id);
//        params.put("password", password);
        params.put("defunct", 'N');
        try {
            User user = iUserDao.selectByParams(params).get(0);
            if (pwCheck(password, user.getPassword())) {
                return user;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public boolean register(User user) {
        Map<String, Object> params = new HashMap<>();
        params.put("user_id", user.getUser_id());
        if (!iUserDao.selectByParams(params).isEmpty()) {
            return false;
        }
        iUserDao.insert(user);
        return true;
    }

    public boolean update(User user, String password) {
        if (login(user.getUser_id(), password) == null) {
            return false;
        }
        iUserDao.update(user);
        return true;
    }

    public String getPrivilege(String user_id) {
        Map<String, Object> param = new HashMap<>();
        param.put("user_id", user_id);
        param.put("rightstr", "administrator");
        if (iPrivilegeDao.selectByParam(param) == null || iPrivilegeDao.selectByParam(param).isEmpty()) {
            return null;
        }
        return "administrator";
    }

    private static boolean pwCheck(String password, String saved) {
        System.out.println(password + "\n" + saved);
        byte[] a = decode(saved);
        byte[] b = new byte[a.length - 20];
        System.arraycopy(a, 20, b, 0, a.length - 20);
        String salt = new String(b);
//        System.out.println(salt);
        password = SHA.Encrypt(password, SHA.MD5);
//        System.out.println(password);
        String hash = Encrypt(password + salt) + salt;
//        System.out.println(new String(decode(saved)));
//        System.out.println(hash);
        return hash.equals(new String(decode(saved)));
    }

    private static String Encrypt(String strSrc) {
        MessageDigest md;
        String strDes;
        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(bt);
            strDes = new String(md.digest());
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return strDes;
    }

    /**
     * 解码
     */
    private static byte[] decode(String str) {
        byte[] bt = null;
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt = decoder.decodeBuffer(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bt;
    }
}
